package com.yuanjin.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

/**
 * @author zhan414
 *传入参数为
 */
public class AsyncFriendShowTask extends AsyncTask<String, Integer, Boolean> {
	private Context context;

	public AsyncFriendShowTask(Context context) {
		this.context = context;
	}

	@Override
	protected Boolean doInBackground(String... params) {
		// Log.e("testing", "------------params[0] =  ------------->>"
		// + params[0]);
		try {
			String sourceUrl = params[0];
			URL url = new URL(sourceUrl);// 创建下载地址对应的URL对象
			HttpURLConnection urlConn = (HttpURLConnection) url
					.openConnection();// 创建一个连接
			urlConn.setConnectTimeout(3000);// 设置连接网络的超时时间
			int response = urlConn.getResponseCode();// 如果服务器完成了客户端请求则会返回一个状态码
			InputStream is = null;
			Log.e("testing", "-------------------->response=" + response);
			if (response == 200) {// 表示成功
				is = urlConn.getInputStream();// 获取输入流对象

			}

			if (is != null) {
				String expandName = sourceUrl.substring(
						sourceUrl.lastIndexOf(".") + 1, sourceUrl.length())
						.toLowerCase();// 获取文件的扩展名
				String fileName = sourceUrl.substring(
						sourceUrl.lastIndexOf("/") + 1,
						sourceUrl.lastIndexOf("."));// 获取文件名
				String SDcardPath = Environment.getExternalStorageDirectory()
						.getPath();
				String pathString = SDcardPath + "/" + "Testing" + "/"
						+ fileName + "." + expandName;
				File fileDirectory = new File(SDcardPath + "/" + "Testing");
				if (!fileDirectory.exists()) {
					// 若不存在，创建目录，可以在应用启动的时候创建
					fileDirectory.mkdirs();
				}
				Log.e("testing", "-------------------->pathString="
						+ pathString);
				File file = new File(pathString);

				FileOutputStream foStream = new FileOutputStream(file);// 创建一个文件输出流对象
				byte buf[] = new byte[2048];// 创建一个字节数组
				// 读取文件到输出流对象中
				while (true) {
					int numread = is.read(buf);
					if (numread <= 0) {
						break;
					} else {
						foStream.write(buf, 0, numread);
					}
				}

				foStream.close();

			}

			/******************************************/
			// InputStreamReader isr=new InputStreamReader(is);
			// BufferedReader br=new BufferedReader(isr);
			// String resultString=br.readLine();
			// Log.e("testing",
			// "------------Inputstream is =  ------------->>"+resultString);
			/******************************************/

			is.close(); // 关闭输入流对象
			urlConn.disconnect(); // 关闭连接
			return true;
		} catch (MalformedURLException e) {
			e.printStackTrace(); // 输出异常信息
			return false;
		} catch (IOException e) {
			e.printStackTrace(); // 输出异常信息
			return false;
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		if (result) {
			Toast.makeText(context, "下载成功", Toast.LENGTH_SHORT).show();
		}else {
			Toast.makeText(context, "下载失败，请重试。。", Toast.LENGTH_SHORT).show();
		}
	}
}
